package com.gxa.service.impl;

import com.gxa.common.JsonResult;
import com.gxa.common.R;
import com.gxa.dao.DepartmentDao;
import com.gxa.dao.impl.DepartmentDaoImpl;
import com.gxa.pojo.Department;
import com.gxa.service.DepartmentService;

import java.sql.SQLException;
import java.util.List;

public class DepartmentServiceImpl implements DepartmentService {
    //创建DepartmentDao对象
    private DepartmentDao departmentDao = new DepartmentDaoImpl();
    @Override
    public JsonResult queryDepartmentList(Integer currentPage, Integer pageSize) {
        //创建JsonResult对象
        JsonResult jsonResult = new JsonResult();
        //调用dao
        try {
            //总记录数
            Integer totalSize = departmentDao.countDepartment();
            //处理 limit 参数
            Integer param01 = (currentPage-1)*pageSize;
            Integer param02 = pageSize;
            List<Department> list = departmentDao.queryDepartmentList(param01, param02);
            //判断结果
            if(list!=null){
                jsonResult.setCode(0);//查询的数据展示在LayUI的数据表格，响应成功的状态码0
                jsonResult.setMsg("查询成功");
                jsonResult.setCount(totalSize);
                jsonResult.setData(list);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差，请联系管理员！");
        }
        return jsonResult;
    }

    @Override
    public R add(Department department, String date) {
        R r = new R();
        try {
            int result = departmentDao.add(department,date);
            if(result>0){
                r.setCode(200);
                r.setMsg("添加成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员！");
        }

        return r;
    }

    @Override
    public R delDepartmentById(String[] ids) {
        R r = new R();
        try {
            if(ids.length>0){
                for (String id : ids) {
                    int result = departmentDao.delDepartment(id);
                }
            }
            r.setCode(200);
            r.setMsg("删除成功");
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员");
        }
        return r;
    }

    @Override
    public R mod(Department department, String date) throws SQLException {
        R r = new R();
        try {
            int result = departmentDao.mod(department,date);
            if (result > 0) {
                r.setCode(200);
                r.setMsg("修改成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员！");
        }
        return r;
    }

    @Override
    public JsonResult searchDepartment(Integer currentPage, Integer pageSize, String name) {
        //创建JsonResult对象
        JsonResult jsonResult = new JsonResult();
        //调用dao
        try {
            //总记录数
            Integer totalSize = departmentDao.countDepartment(name);
            //处理 limit 参数
            Integer param01 = (currentPage-1)*pageSize;
            Integer param02 = pageSize;
            List<Department> list = departmentDao.searchDepartment(param01, param02,name);
            //判断结果
            if(list!=null){
                jsonResult.setCode(0);//查询的数据展示在LayUI的数据表格，响应成功的状态码0
                jsonResult.setMsg("查询成功");
                jsonResult.setCount(totalSize);
                jsonResult.setData(list);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差，请联系管理员！");
        }
        return jsonResult;
    }
}
